<template>
  <el-dialog :title="title" :visible="show" @close="$emit('update:show',false)">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="开启" prop="enVisible">
        <!-- el-switch active-value打开时的值 -->
        <!-- inactive-value关闭时的值 -->
        <el-switch v-model="form.enVisible" active-value="1" inactive-value="0" /></el-form-item>
    </el-form>

    <template #footer>
      <div class="center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="$emit('update:show',false)">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission'
export default {
  props: {
    show: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      isShow: false,
      mode: 'add',
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '0', //	string	非必须  0：关半  1：打开
        type: '', //	number	非必须  1:页面 2：按钮
        pid: '' //	string	非必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        code: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须
      }
    }
  },
  computed: {
    title() {
      return {
        add: '新增权限点',
        edit: `编辑【${this.form.name}】权限点`
      }[this.mode]
    }
  },
  watch: {
    show: {
      handler(newVal) {
        if (newVal === false) {
          this.closeEvent()
        }
      }
    }
  },
  methods: {
    async  submit() {
      await this.$refs.form.validate()
      if (this.mode === 'add') {
        await sysPermissionPost(this.form)
        this.$message.success('新增权限点成功')
      } else if (this.mode === 'edit') {
        await sysPermissionPut(this.form)
        this.$message.success('编辑权限点成功')
      }
      this.$emit('update:show', false)
      // this.isShow = false
      this.$emit('getData')
    },
    // 重置表单
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '0', //	string	非必须  0：关半  1：打开
        type: '', //	number	非必须  1:页面 2：按钮
        pid: '' //	string	非必须
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.center{
    text-align: center;
}
</style>
